<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * connections
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class connections extends MY_Model {
    
    
    
    protected $_arrEncrypt = array(
        'host',
        'username',
        'password',
    );
    
    
    
    
    
    
    /**
     * Decode
     * 
     * Decode the connection.  This decodes everything
     * with your username.
     * 
     * If you wish to change it to authenticate with
     * another string, do so here and in the _decode()
     * method.
     * 
     * NB. If you encode it with something that's not
     * available globally, then you won't be able to
     * use your connections on all instances of
     * VanillaDB.
     * 
     * @param mixed $arrConnections
     * @return mixed 
     */
    protected function _decode($arrConnections) {
        $username = 'riggerthegeek';
        
        if(is_multi_array($arrConnections)) {
            /* Multi level array */
            if(count($arrConnections) > 0) {
                foreach($arrConnections as $key => $value) {
                    $arrConnections[$key] = $this->_decode($value);
                }
            }
            return $arrConnections;
        } elseif(is_array($arrConnections)) {
            /* Array */
            if(count($arrConnections) > 0) {
                foreach($this->_arrEncrypt as $type) {
                    if(array_key_exists($type, $arrConnections)) {
                        $arrConnections[$type] = $this->_decode($arrConnections[$type]);
                    }
                }
            }    
            return $arrConnections;
        } else {
            /* String */
            return $this->encrypt->decode($arrConnections, $username);
        }
    }
    
    
    
    
    
    
    
    /**
     * Encode
     * 
     * Encode the connection.  This encodes everything
     * with your username.
     * 
     * If you wish to change it to authenticate with
     * another string, do so here and in the _encode()
     * method.
     * 
     * NB. If you encode it with something that's not
     * available globally, then you won't be able to
     * use your connections on all instances of
     * VanillaDB.
     * 
     * @param mixed $arrConnections
     * @return mixed
     */
    protected function _encode($arrConnections) {
        $username = 'riggerthegeek';
        
        if(is_multi_array($arrConnections)) {
            /* Multi level array */
            if(count($arrConnections) > 0) {
                foreach($arrConnections as $key => $value) {
                    $arrConnections[$key] = $this->_encode($value);
                }
            }
            return $arrConnections;
        } elseif(is_array($arrConnections)) {
            /* Array */
            if(count($arrConnections) > 0) {
                foreach($this->_arrEncrypt as $type) {
                    if(array_key_exists($type, $arrConnections)) {
                        $arrConnections[$type] = $this->_encode($arrConnections[$type]);
                    }
                }
            }
            return $arrConnections;
        } else {
            /* String */
            return $this->encrypt->encode($arrConnections, $username);
        }
    }
    
    
    
    
    
    
    
    
    /**
     * Decode
     * 
     * Decode the connection array/string
     * 
     * @param array/string $connection
     * @param boolean $use_key
     * @return mixed
     */
    public function decode($connection, $use_key = true) {
        
        if(is_array($connection)) {
            /* Array */
            if(count($connection) > 0) {
                if($use_key) {
                    /* Data we're looking for is in the key */
                    $connection = array_keys($connection);
                }
                
                $arrReturn = array();
                
                foreach($connection as $encode) {
                    
                    $decode = $this->decode($encode);
                    if($decode !== false) {
                        $arrReturn[$encode] = $decode;
                    }
                }
                
                if(count($arrReturn) > 0) {
                    return $arrReturn;
                }
            }
            
        } else {
            /* String */
            $this->load->helper('string');
            $this->load->library('encrypt');
            
            /* Decode the connection */
            $connection = $this->encrypt->decode($connection);
            
            /* Is it serialized */
            if(is_serialized($connection)) {
                $arrConnection = unserialize($connection);
                
                $arrKeys = array('type', 'host', 'username', 'password', 'port');
                
                if(array_keys_exist($arrKeys, $arrConnection)) {
                    /* 'Name' is not compulsory */
                    return $arrConnection;
                }
            }
        }
        
        return false;
        
    }
    
    
    
    
    
    
    
    
    /**
     * Encode
     * 
     * Encode the connections so they can be put into
     * an array
     * 
     * @param array $arrConnection
     * @return mixed 
     */
    public function encode(array $arrConnection = array()) {
        
        if(is_multi_array($arrConnection)) {
            /* If multi array, cycle through each one */
            if(count($arrConnection) > 0) {
                $arrReturn = array();
                foreach($arrConnection as $key => $connection) {
                    $encode = $this->encode($connection);
                    if($encode !== false) {
                        $arrReturn[] = $encode;
                    }
                }
                
                if(count($arrReturn) > 0) {
                    return $arrReturn;
                } else {
                    return false;
                }
            }
        } else {
            /* Ordinary array - encode it */
            $arrKeys = array('type', 'host', 'username', 'password', 'port');
            
            if(array_keys_exist($arrKeys, $arrConnection)) {
                /* Encode the connection data */
                $this->load->library('encrypt');
                
                $name = $arrConnection['name'];
                
                $key = $this->encrypt->encode(serialize($arrConnection));
                
                $arrReturn = array(
                    'key' => $key,
                    'name' => $name,
                );
                
                return $arrReturn;
            }
        }
        
        /* Error occured here */
        return false;
        
    }
    
    
    
    
    
    
    
    
    /**
     * Get Saved Connections
     * 
     * Gets the saved connections
     * 
     * @return array
     */
    public function get_saved_connections($not_connected = false) {
        
        $arrConnections = array();
        
        if($this->api_connect->can_connect() && $this->api_connect->is_logged_in()) {
            $arrList = $this->api_connect->call('GET', 'connections/list');

            if(array_key_exists('connections', $arrList)) {
                $arrConnections = $this->_decode($arrList['connections']);
                
                if($not_connected && count($arrConnections) > 0) {
                    /* Get only ones that aren't connected */
                    $this->_load_model('database');
                    
                    /* Get our existing connections */
                    $arrSaved = $this->database->fetch_connections();
                    
                    if(count($arrSaved) > 0) {
                        $arrSearch = array();
                        foreach($arrSaved as $saved) {
                            $arrSearch = array(
                                'name' => $saved['name'],
                                'type' => $saved['type'],
                                'host' => $saved['server'],
                                'username' => $saved['username'],
                                'password' => $saved['password'],
                                'port' => $saved['port'],
                            );
                            
                            $id = array_search($arrSearch, $arrConnections);
                            if($id !== false) {
                                unset($arrConnections[$id]);
                            }
                        }
                        
                    }
                    
                }
            }
        }
        
        return $arrConnections;
        
    }



}
?>